package com.yao.fin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yao.common.annotation.User;
import com.yao.common.bean.PageQueryBean;
import com.yao.common.helper.ValidateHelper;
import com.yao.common.resp.ApiResponse;
import com.yao.fin.entity.FinancialTemplateEntity;
import com.yao.fin.entity.dto.FinancialTemplateDto;
import com.yao.fin.entity.qo.FinancialTemplateQo;
import com.yao.fin.enums.CourseBase;
import com.yao.fin.enums.LabelEnum;
import com.yao.fin.service.FinancialTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会计课堂管理 Controller
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-03-12
 */
@Api(tags = "会计课堂管理")
@RestController
@RequestMapping(value = "/financialTemplate")
public class FinancialTemplateController {

    private static Logger logger = LoggerFactory.getLogger(FinancialTemplateController.class);

    @Autowired
    private FinancialTemplateService financialTemplateService;

    @ApiOperation(value = "新增练习题", notes = "新增练习题")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Object> add(FinancialTemplateDto model) {
        logger.info("add.model={}", model);

        return ApiResponse.success(financialTemplateService.save(model));
    }

    @ApiOperation(value = "修改练习题", notes = "修改练习题")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Object> update(FinancialTemplateDto model) {
        Console.log("修改账户:{}", model);
        return ApiResponse.success(financialTemplateService.updateById(model));
    }

    @ApiOperation(value = "删除练习题", notes = "逻辑删除练习题")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ApiResponse<Object> deleteById(@PathVariable("id") Integer id) {
        return ApiResponse.success();
    }

    @ApiOperation(value = "获取详情", notes = "获取详情")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ApiResponse<FinancialTemplateEntity> detail(@PathVariable("id") Integer id) {

        return ApiResponse.success(financialTemplateService.getById(id));
    }

    @ApiOperation(value = "练习题列表（分页）", notes = "列表查询")
    @PostMapping("/list")
    public ApiResponse<IPage<FinancialTemplateDto>> list(@RequestBody PageQueryBean<FinancialTemplateQo> queryBean) {
        if (queryBean.getData() == null) {
            queryBean.setData(new FinancialTemplateQo());
        }

        return ApiResponse.success(financialTemplateService.listpage(queryBean, queryBean.getData()));
    }

    @ApiOperation(value = "科目查询", notes = "科目查询")
    @GetMapping("/listCourse/{label}")
    public ApiResponse<List<Map<String, String>>> listCourse(@PathVariable("label") Integer label) {

        CourseBase.ZJEnum.getEnums();

        List<Map<String, String>> list = CollUtil.newArrayList();

        if (label == LabelEnum.ZJ.getCode()) {
            CourseBase.ZJEnum.getEnums().forEach((k, v) -> {
                HashMap<String, String> map = CollUtil.newHashMap();

                map.put("id", k.toString());
                map.put("name", v);
                list.add(map);
            });
        }else if(label == LabelEnum.CPA.getCode()){
            CourseBase.CPAEnum.getEnums().forEach((k, v) -> {
                HashMap<String, String> map = CollUtil.newHashMap();

                map.put("id", k.toString());
                map.put("name", v);
                list.add(map);
            });
        }

        return ApiResponse.success(list);
    }

    @ApiOperation(value = "导出", notes = "导出")
    @PostMapping("/export")
    public ApiResponse<Object> export(FinancialTemplateQo qo) {

        ValidateHelper.validateNull(qo, new String[]{"label"});
        Console.log(" 导出 文本 条件：{}", qo);

        return ApiResponse.success(financialTemplateService.export(qo));
    }



    @ApiOperation(value = "练习题列表（分页）", notes = "列表查询")
    @PostMapping("/list_test")
    public ApiResponse<IPage<FinancialTemplateDto>> test(IPage<FinancialTemplateQo> queryBean) {

        Console.log("当前页面 = = "+queryBean.getCurrent());


        return ApiResponse.success();
    }
}